package top.lshaci.learning.springboot.web.fastjson.writer;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.codec.DateTimeCodec;
import com.alibaba.fastjson2.writer.ObjectWriter;

import java.lang.reflect.Type;
import java.time.LocalDate;
import java.util.Locale;

/**
 * ObjectWriterLocalDate
 *
 * @author liangxu
 */
public class ObjectWriterLocalDate extends DateTimeCodec implements ObjectWriter<LocalDate> {

    public static final ObjectWriterLocalDate INSTANCE = new ObjectWriterLocalDate(null, null);

    public ObjectWriterLocalDate(String format, Locale locale) {
        super(format, locale);
    }

    @Override
    public void writeJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        jsonWriter.writeLocalDate((LocalDate) object);
    }

    @Override
    public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        if (object == null) {
            jsonWriter.writeNull();
            return;
        }

        LocalDate date = (LocalDate) object;
        jsonWriter.writeString(date.toString());
    }
}
